﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Diagnostics;

namespace cinetoona
{
    public partial class Reservation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["IsAuthenticated"] == null)
            //{
            //    Response.Redirect("Login.aspx");
            //}
            if (Session["ConnectionString"] != null)
            {
                if (!Page.IsPostBack)
                {
                    //DataAccessDataContext cinetoonadb = new DataAccessDataContext(Session["ConnectionString"].ToString());
                    using (DataAccessDataContext cinetoonadb = new DataAccessDataContext(Session["ConnectionString"].ToString()))
                    {
                        //var joinShowMovies = (from m in cinetoonadb.Movies
                        //                     join s in cinetoonadb.Shows on m.MovieID equals s.MovieID
                        //                     select new { Title = s.Movies.Title }).Distinct();

                        //var getMovies = from m in cinetoonadb.Movies
                        //                select new { m.Title, m.MovieID };

                        //var test = from c in cinetoonadb.Bookings
                        //           select new { c.Movies.Title, c.MovieID };

                        var shows = from s in cinetoonadb.Shows
                                    select new { s.Movies.Title, s.MovieID };

                        ShowDropDownList.DataSource = shows.Distinct();
                        ShowDropDownList.DataTextField = "Title";
                        ShowDropDownList.DataValueField = "MovieID";
                        ShowDropDownList.DataBind();

                        var getShowDateTime = from s in cinetoonadb.Shows
                                              where s.MovieID == Convert.ToInt32(ShowDropDownList.SelectedValue)
                                              select new { s.Start, s.ShowID };

                        DateDropDownList.DataSource = getShowDateTime;
                        DateDropDownList.DataTextField = "Start";
                        DateDropDownList.DataValueField = "ShowID";
                        DateDropDownList.DataBind();

                        //QuantityTextBox.Text = ShowDropDownList.SelectedValue;

                        //foreach (var s in getActiveMovies)
                        //{
                        //    ShowDropDownList.Items.Add(s.);
                        //}
                    }
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void getReservation_Click(object sender, EventArgs e)
        {
            try
            {
                using (DataAccessDataContext cinetoonadb = new DataAccessDataContext(Session["ConnectionString"].ToString()))
                {
                    //Booking bookingObj = cinetoonadb.Bookings.First(o => o.Phone == Int32.Parse(this.reservationnumber.Text));
                    //Show relatedShow = cinetoonadb.Shows.First(o => o.ShowID == bookingObj.ShowID);

                    var getReservation = from b in cinetoonadb.Bookings
                                         where b.Phone == Convert.ToInt32(reservationnumber.Text) && b.Payed == false
                                         select new
                                         {
                                             b.BookingID,
                                             b.MovieID,
                                             Title = b.Movies.Title,
                                             b.Name,
                                             b.Quantity,
                                             b.Phone,
                                             b.Payed,
                                         };

                    ReservationGridView.DataSource = getReservation;
                    ReservationGridView.DataBind();
                }
            }
            catch (Exception g) { Console.WriteLine(g.Message); }

        }

        protected void BookButton_Click(object sender, EventArgs e)
        {
            using (DataAccessDataContext cinetoonadb = new DataAccessDataContext(Session["ConnectionString"].ToString()))
            {
                Booking booking = new Booking();
                booking.MovieID = Convert.ToInt32(ShowDropDownList.SelectedValue);
                booking.ShowID = Convert.ToInt32(DateDropDownList.SelectedValue);
                booking.Name = NameTextBox.Text;
                booking.Phone = Convert.ToInt32(PhoneTextBox.Text);
                booking.Quantity = Convert.ToInt32(QuantityTextBox.Text);
                booking.Payed = false;

                cinetoonadb.Bookings.InsertOnSubmit(booking);
                cinetoonadb.SubmitChanges();
            }
        }

        protected void ShowDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DataAccessDataContext cinetoonadb = new DataAccessDataContext(Session["ConnectionString"].ToString()))
            {
                var getShowStart = from s in cinetoonadb.Shows
                                   where s.MovieID == Convert.ToInt32(ShowDropDownList.SelectedValue)
                                   select new { s.Start, s.ShowID };

                DateDropDownList.DataSource = getShowStart;
                DateDropDownList.DataTextField = "Start";
                DateDropDownList.DataValueField = "ShowID";
                DateDropDownList.DataBind();
            }
        }

        protected void print_Click(object sender, EventArgs e)
        {
            if (ReservationGridView.SelectedRow == null)
            {

            }
            else
            {
                Document myDocument = new Document(PageSize.A4.Rotate());
                String timestamp = DateTime.Now.ToString();
                timestamp = timestamp.Replace('/', '-');
                timestamp = timestamp.Replace(':', '-');
                try
                {
                    PdfWriter.GetInstance(myDocument, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\TICKET" + timestamp + ".pdf", FileMode.Create));
                    myDocument.Open();
                    myDocument.Add(new Paragraph("# Sample Movie Ticket", FontFactory.GetFont("Courier", 12.0f, BaseColor.BLACK)));
                    String details = "";
                    int i = 0;
                    while (i < ReservationGridView.SelectedRow.Cells.Count)
                    {
                        if (!ReservationGridView.Columns[i].HeaderText.Equals("Quantity") &&!ReservationGridView.SelectedRow.Cells[i].Text.Equals(""))
                        {
                            details += "# " + ReservationGridView.SelectedRow.Cells[i].Text + " \n ";
                        }
                        i++;
                    }
                    myDocument.Add(new Paragraph(details,FontFactory.GetFont("Courier", 12.0f, BaseColor.BLACK)));
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                }

                // step 5: Remember to close the documnet

                myDocument.Close();
                Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\TICKET" + timestamp + ".pdf");
            }
        }
    }
}